package com.sky.controller.user;

import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController("userDishController")
@Slf4j
@RequestMapping("/user/dish")
public class DishController {

    @Resource
    private DishService dishService;
    /*
    * 根据分类id查询菜品
    * */
    @Resource
    private RedisTemplate redisTemplate;
    @GetMapping("/list")
    public Result<List<DishVO>> list(@RequestParam Long categoryId){
        log.info("根据分类id查询菜品:{}",categoryId);
        String key = "dish_" + categoryId;
        List<DishVO> dishVOS = (List<DishVO>)redisTemplate.opsForValue().get(key);
        if(dishVOS != null){
            return Result.success(dishVOS);
        }
        List<DishVO> dishes = dishService.queryWithFlavors(categoryId);
        redisTemplate.opsForValue().set(key,dishes);
        return Result.success(dishes);
    }
}
